const orm = require('../util/connect').orm()

const MODEL_INIT_LIST = [
    'consumer',
    'employee',
    'goods',
    'login',
    'order',
    'trans',
    'user',
]

const MODELS = []

for (const x of MODEL_INIT_LIST) {
    MODELS.push(orm.import('./models/' + x))
}

async function forceSyncModels(MODELS) {
    for (const model of MODELS.reverse()) {
        await model.sync({ force: true })
    }
}

async function forceDropModels(MODELS) {
    for (const model of MODELS.reverse()) {
        await model.drop()
    }
}

orm
    .authenticate()
    .then(async () => {
        console.log('Connection has been established successfully.')
        await forceDropModels(MODELS)
        await forceSyncModels(MODELS) // 坑：reverse 是作用在原数组上的，不是搞了个新的返回
        console.log('Sync() succeed.')
        console.log('导入默认订单数据……')
        const order = await orm.import('./models/order')
        await order.create({ location: '上海', father: null })
        await order.create({ location: '宝山', father: '上海' })

        console.log('新建管理员账户……')
        const employee = await orm.import('./models/employee')
        await employee.create({
            name: '孙雪婷',
            phone: '13000000000',
        })
            .then(async user => {
                const admin = await orm.import('./models/user')
                await user.update({
                    password: 'jxtxzzw_passwordSuffix_RgKR*%fWKi44L3Z511340d3c0c330288ba9cc2680467357a'
                }, {
                    where: {
                        user_name: user.user_name
                    }
                })
                    .then(async () => {
                        // const HR = await orm.import('./models/HumanResources')
                        // await HR.create({
                        //     hr_id: member.user_name
                        // })
                        // const RC = await orm.import('./models/Receptionists')
                        // await RC.create({
                        //     receptionist_id: member.user_name
                        // })
                        // const TP = await orm.import('./models/Transports')
                        // await TP.create({
                        //     transport_id: member.user_name
                        // })
                        // const WH = await orm.import('./models/WareHouseManagers')
                        // await WH.create({
                        //     manager_id: member.user_name
                        // })
                        // const DP = await orm.import('./models/Dispatchers')
                        // await DP.create({
                        //     uuid: member.user_name
                        // })
                        // console.log('=====================================================')
                        // console.log('初始化完成，请不要再次执行该函数，否则会清空所有数据并重新建表！')
                        // console.log('超级管理员：admin@jxtxzzw.com，密码：jxtxzzw')
                        // console.log('超级管理员具有所有权限，一旦完成初始信息录入，建议立即废止账户！')
                        // process.exit()
                    })
            })
    })
    .catch(err => {
        console.error('Unable to connect to the database:', err)
    })
